package com.nanyang.tms.service.dataimport;

import java.util.ArrayList;
import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;import com.nanyang.tms.api.dto.request.dataimport.DataImportLineRequest;
import com.nanyang.tms.api.dto.request.dataimport.DataImportRequest;
import com.nanyang.tms.api.dto.request.dataimport.DataImportResultLineQueryRequest;
import com.nanyang.tms.api.dto.request.dataimport.DataImportResultQueryRequest;
import com.nanyang.tms.api.dto.request.dataimport.DataImportUpdateRequest;
import com.nanyang.tms.api.dto.response.dataimport.DataImportResultLineResponse;
import com.nanyang.tms.api.dto.response.dataimport.DataImportResultResponse;
import com.nanyang.tms.api.service.dataimport.DataImportRpcService;
import com.nanyang.tms.domain.dataimport.DataImportDO;
import com.nanyang.tms.domain.dataimport.DataImportLineDO;
import com.nanyang.tms.domain.dataimport.DataImportLineQuery;
import com.nanyang.tms.domain.dataimport.DataImportLineVO;
import com.nanyang.tms.domain.dataimport.DataImportQuery;
import com.nanyang.tms.domain.dataimport.DataImportVO;
import com.nanyang.tms.manager.dataimport.DataImportManager;
import com.nanyang.tms.mongo.DataImportDao;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class DataImportRpcServiceImpl implements DataImportRpcService{
	@Autowired
	private DataImportManager dataImportManager;
	@Autowired
	private DataImportDao dataImportDao;
	@Override
	public ApiResult<Boolean> saveImportFile(DataImportRequest dataImportRequest) {
		 ApiResult<Boolean>  apiResult=new  ApiResult<Boolean> ();
		 try {
			 DataImportDO dataImportDO = new DataImportDO();
			 BeanUtils.copyProperties(dataImportRequest, dataImportDO);
			 dataImportManager.saveImportFile(dataImportDO);
			 apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			 apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			 apiResult.setData(true);
			
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", dataImportRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
		 
	}
	@Override
	public ApiResult<Boolean> updateImportFileReuslt(DataImportUpdateRequest dataImportUpdateRequest) {
		 ApiResult<Boolean>  apiResult=new  ApiResult<Boolean> ();
		 try {
			 DataImportDO dataImportDO = new DataImportDO();
			 List<DataImportLineDO> successLines=new ArrayList<DataImportLineDO>();
			 List<DataImportLineDO> failLines=new ArrayList<DataImportLineDO>();
			 BeanUtils.copyProperties(dataImportUpdateRequest, dataImportDO);
			 BeanUtils.copyListProperties(dataImportUpdateRequest.getSuccessLines(), successLines, DataImportLineDO.class);
			 BeanUtils.copyListProperties(dataImportUpdateRequest.getFailLines(), failLines, DataImportLineDO.class);
			 dataImportDO.setSuccessLines(successLines);
			 dataImportDO.setFailLines(failLines);
			 dataImportDao.updateDataImport(dataImportDO);
			 apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			 apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			 apiResult.setData(true);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", dataImportUpdateRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}
	@Override
	public ApiResult<PageBean<DataImportResultResponse>> listDataImportResult(
			DataImportResultQueryRequest dataImportResultQueryRequest) {
		
		ApiResult<PageBean<DataImportResultResponse>>  result =new ApiResult<PageBean<DataImportResultResponse>> ();
		try {
			DataImportQuery query=new DataImportQuery();
			BeanUtils.copyProperties(dataImportResultQueryRequest, query);
			PageBean<DataImportVO> listDataImport = dataImportManager.listDataImport(query);
			PageBean<DataImportResultResponse>  resPage=new PageTableBean<>(dataImportResultQueryRequest.getPageIndex(), dataImportResultQueryRequest.getPageSize());
			resPage.setTotalItem(listDataImport.getTotalItem());
			BeanUtils.copyListProperties(listDataImport.getData(), resPage.getData(), DataImportResultResponse.class);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(resPage);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", dataImportResultQueryRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}
	@Override
	public ApiResult<PageBean<DataImportResultLineResponse>> listDataImportResultLine(
			DataImportResultLineQueryRequest dataImportResultLineQueryRequest) {
		ApiResult<PageBean<DataImportResultLineResponse>>  result =new ApiResult<PageBean<DataImportResultLineResponse>> ();
		
		try {
			DataImportLineQuery query=new DataImportLineQuery();
			BeanUtils.copyProperties(dataImportResultLineQueryRequest, query);
			PageBean<DataImportLineVO> page = dataImportManager.listDataImportLine(query);
			PageBean<DataImportResultLineResponse>  resPage=new PageTableBean<>(dataImportResultLineQueryRequest.getPageIndex(), dataImportResultLineQueryRequest.getPageSize());
			resPage.setTotalItem(page.getTotalItem());
			BeanUtils.copyListProperties(page.getData(), resPage.getData(), DataImportResultLineResponse.class);
			result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			result.setData(resPage);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", dataImportResultLineQueryRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
		}
		return result;
	}
}
